home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / ENUMROT.CPP < prev    next >
C/C++ Source or Header  |  1993-06-06  |  6KB  |  154 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 06-04-93 (13:45)             Number: 50
  4. From: UENAL MUTLU                  Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: MKENUM: Cycling through a      Conf: (37) C++ Langua
  7. ---------------------------------------------------------------------------
  8. //--------------------------------------------------------------------
  9. #if 0  /*
  10.    ENUMROT.CPP - Enum Rotator v1.00
  11.    Written 930603 by Uenal Mutlu
  12.  
  13.    Iterating over all possible values of an enum using a macro and an
  14.    auto-created int-array therein. The enum is fully type-safe and also
  15.    auto-created in the macro.
  16.  
  17.    Example for enums with max 10 'members' N0..N9 with Values V0..V9
  18.    (some manual work is still neeeded for other count of elems :-( )
  19.    Tip: Use all args (fill up) but set Nelems to the actual count.
  20.         For iterating etc. use
  21.           Name##_Elems    nr of elems
  22.           Name##_Tbl[i]   int array (use only 0..Name##_Elems - 1)
  23.         (both are auto-created in MKENUM)
  24.  
  25.    Sample Usage:
  26.  
  27.      // this macro creates the enum MyEnum with 3 values
  28.      MKENUM(MyEnum, 3,  red,300, green,100, blue,400,   /* 3 vals */
  29.                         N3,0, N4,0, N5,0, N6,0, N7,0,   /* filler */
  30.                         N8,0, N9,0);                    /* filler */
  31.  
  32.      // usages
  33.      MyEnum Color = green;
  34.      MyEnum Bad   = 5;   // BC++3.1 correctly warns such bad usage
  35.  
  36.      printf("MyEnum has %d user values \n", MyEnum_Nelems);
  37.  
  38.      // iterating over all values (original sort order)
  39.      for (int i = 0; i < MyEnum_Nelems; i++)
  40.        printf("EnumVal%d == %d \n", i, MyEnum_Tbl[i]);
  41.  
  42.      // Output:
  43.      //   EnumVal0 == 300
  44.      //   EnumVal1 == 100
  45.      //   EnumVal2 == 400
  46.  
  47.    Improvements: one could avoid the static array and create it via a
  48.    dyamically (hick :-) defined class template (hick :-) with member-
  49.    initializers etc. So the instance could be created on the stack and
  50.    at need only... Perhaps then var-args could be implemented too...
  51.  
  52.    BTW: this method also prevents from a compiler (perhaps language)
  53.         bug! Look at the following bad behaviour of my compiler:
  54.  
  55.         enum TENUM
  56.           {
  57.             Fruehling = -10,
  58.             Sommer    =  10,
  59.             Herbst    =  20,
  60.             Winter    =  30
  61.           } EA, EB = Herbst;  // default initval IS 0 !!!
  62.  
  63.        Here, EA has the value 0! Though it's not a valid value!
  64.        Is this a compiler-bug or lack in the language? IMO the
  65.        default initializator should be the first value, here -10...
  66.  
  67.     History:
  68.     --------
  69.     930603ThUm v1.00 posted to comp.lang.cpp
  70.                (Thread: "Cycling through an enum's states")
  71.     930604FrUm posted to C_PLUSPLUS on FidoNet
  72.  
  73. -
  74. Sysop of RCI-BBS Munich/D.        Bbs/FidoEMail/Fax: +49-89-8002175
  75. EMail : Uenal_Mutlu@p0.f17.n246.z2.{ fido.de, fidonet.org }
  76. FidoID: Uenal Mutlu 2:246/17
  77. A "Gastarbeiter" living 20 years in GERMANY has still ZERO-RIGHTS!!!
  78. -
  79. */
  80. #endif
  81.  
  82. //--------------------------------------------------------------------
  83. // THE 'MAGIC' MACRO  "MKENUM":
  84. //--------------------------------------------------------------------
  85. #define MKENUM_MAX_ELEMS   10
  86.  
  87. #define MKENUM(Name, Nelems,  N0,V0, N1,V1, N2,V2, N3,V3, N4,V4,  \
  88.                               N5,V5, N6,V6, N7,V7, N8,V8, N9,V9)  \
  89. enum Name { N0 = V0, N1 = V1, N2 = V2, N3 = V3, N4 = V4,          \
  90.             N5 = V5, N6 = V6, N7 = V7, N8 = V8, N9 = V9,          \
  91.             Name##_Nelems = Nelems };  /* use this for actual nelems */ \
  92. static int const Name##_Tbl[MKENUM_MAX_ELEMS] = { N0, N1, N2, N3, N4,   \
  93.                                                   N5, N6, N7, N8, N9 }
  94.  
  95. // since all enum values, nelem and the table_name is known one can
  96. // iterate simply over all possible values.
  97.  
  98. //--------------------------------------------------------------------
  99. // TEST PART:
  100. //--------------------------------------------------------------------
  101. #if 1
  102.  
  103. #include <stdio.h>
  104.  
  105. // sample usage:
  106. MKENUM(MyEnum, 3,  red,300, green,100, blue,400,               /* 3 used */
  107.                    N3,0, N4,0, N5,0, N6,0, N7,0, N8,0, N9,0);  /* filler */
  108.  
  109. int main()
  110.   {
  111.     MyEnum eColor = green;
  112.     MyEnum eBad   = 5;   // BC++3.1 correctly warns such bad usage (s.b.)
  113.  
  114.     // iterating
  115.     printf("MyEnum has %d user values \n", MyEnum_Nelems);
  116.     for (int i = 0; i < MyEnum_Nelems; i++)
  117.       printf("Val%d == %d \n", i, MyEnum_Tbl[i]);
  118.  
  119.  
  120.     // dummy to shut-up the compiler warnings "eColor, eBad  not used"...
  121.     int rc = eColor == eBad;
  122.  
  123.     return rc;
  124.   }
  125.  
  126. #endif
  127.  
  128. //--------------------------------------------------------------------
  129. // COMPILE AND RUN:
  130. //--------------------------------------------------------------------
  131. /*
  132. Compile: bcc -ml enumrot.cpp
  133. --------
  134. Borland C++  Version 3.1 Copyright (c) 1992 Borland International
  135. enumrot.cpp:
  136. Warning enumrot.cpp 89: Initializing MyEnum with int in function main()
  137. Turbo Link  Version 5.1 Copyright (c) 1992 Borland International
  138.  
  139. Run:
  140. ----
  141. MyEnum has 3 user values
  142. Val0 == 300
  143. Val1 == 100
  144. Val2 == 400
  145. */
  146.  
  147. //--------------------------------------------------------------------
  148.  
  149. --- DB 1.53/001602
  150.  * Origin: RCI-BBS Munich/D [+49-89-8002175] (2:246/17)
  151. SEEN-BY: 1/211 11/2 4 13/13 101/1 109/25 114/5 123/19 124/1 153/752 154/40
  152. SEEN-BY: 154/77 157/110 159/100 125 140 180 270 430 575 950 203/23 209/209
  153. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2440/5 3603/20
  154.